package com.abe.app;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import org.xutils.DbManager;
import org.xutils.x;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android_serialport_api.SerialPort;

import com.abe.config.Constants;
import com.abe.config.ReadWriteFile;
import com.abe.config.SerialPortConstants;

public class ManagerAPP extends Application implements
		Thread.UncaughtExceptionHandler {
	public static ManagerAPP app;
	/** 串口 */
	private SerialPort ttys0_SerialPort = null;
	private SerialPort ttys3_SerialPort = null;
	/** DB */
	private DbManager.DaoConfig daoConfig;
	/** activity列表 */
	private List<Activity> list = new ArrayList<Activity>();

	public static ReadWriteFile TXT;

	public static ManagerAPP getInstance() {
		if (null == app) {
			app = new ManagerAPP();
		}
		return app;
	}

	public DbManager.DaoConfig getDaoConfig() {
		return daoConfig;
	}

	@SuppressWarnings("static-access")
	@Override
	public void onCreate() {
		super.onCreate();
		app = ManagerAPP.this;
		/** 采集错误 */
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
		/** XUtils */
		x.Ext.init(this);
		/** 数据库更新操 */

		daoConfig = new DbManager.DaoConfig().setDbName("abe_db")// 创建数据库的名称
				.setDbVersion(1)// 数据库版本号
				.setDbUpgradeListener(new DbManager.DbUpgradeListener() {
					@Override
					public void onUpgrade(DbManager db, int oldVersion,
							int newVersion) {
						// db.addColumn(...);
						// db.dropTable(...);
						// ...
					}
				});
		String filePath = Constants.getRootPath();
		// String filePath = "/System/";
		try {
			TXT = new ReadWriteFile(filePath);
			TXT.creatTxtFile();
			TXT.writeTxtFile(filePath);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * */
	public static Context getContext() {
		return app.getApplicationContext();
	}

	/**
	 * 删除
	 */
	public void remove(Activity activity) {
		list.remove(activity);
	}

	/**
	 * 添加到栈
	 */
	public void addActivity(Activity activity) {
		list.add(activity);
	}

	/**
	 * 完全退出
	 */
	public void exit(Context context) {
		SharedPreferences preferences_it;
		SharedPreferences.Editor editor_it;
		preferences_it = getSharedPreferences("itcase", Context.MODE_PRIVATE);
		editor_it = preferences_it.edit();
		editor_it.putBoolean("is_quit", true);
		editor_it.commit();
		for (Activity activity : list) {
			activity.finish();
		}
		System.exit(0);
	}

	@Override
	public void uncaughtException(Thread arg0, Throwable arg1) {
	}

	public SerialPort getSerialPort(int type) throws SecurityException,
			IOException, InvalidParameterException {
		if (type == SerialPortConstants.SerialPort_ttys0) {
			if (ttys0_SerialPort == null) {
				ttys0_SerialPort = new SerialPort(new File(
						SerialPortConstants.path_ttys0),
						SerialPortConstants.baudrate, 0);
			}
			return ttys0_SerialPort;
		} else if (type == SerialPortConstants.SerialPort_ttys3) {
			if (ttys3_SerialPort == null) {
				ttys3_SerialPort = new SerialPort(new File(
						SerialPortConstants.path_ttys3),
						SerialPortConstants.baudrate, 0);
			}
			return ttys3_SerialPort;
		} else {
			return null;
		}
	}

	public void closeSerialPort(int type) {
		if (type == SerialPortConstants.SerialPort_ttys0
				&& ttys0_SerialPort != null) {
			ttys0_SerialPort.close();
			ttys0_SerialPort = null;
		}
		if (type == SerialPortConstants.SerialPort_ttys3
				&& ttys3_SerialPort != null) {
			ttys3_SerialPort.close();
			ttys3_SerialPort = null;
		}
	}
}
